package org.nutz.j2cache.shiro;

import org.apache.shiro.ShiroException;
import org.apache.shiro.cache.Cache;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.util.Destroyable;
import org.apache.shiro.util.Initializable;

import net.oschina.j2cache.CacheChannel;
import net.oschina.j2cache.J2Cache;

/**
 * 适配Shiro的CacheManager
 * @author wendal
 *
 */
public class J2CacheManager implements CacheManager, Initializable, Destroyable {
	
	protected CacheChannel channel;

	public <K, V> Cache<K, V> getCache(String name) throws org.apache.shiro.cache.CacheException {
		return new ShiroJ2Cache<K, V>(name, channel);
	}

	public void init() throws ShiroException {
		channel = J2Cache.getChannel();
	}

	public void destroy() throws Exception {
		if (channel != null)
			channel.close();
	}

}
